package com.eqdd.yiqidian.ui;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.arlib.floatingsearchview.FloatingSearchView;
import com.arlib.floatingsearchview.suggestions.model.SearchSuggestion;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.SearchActivityCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.bean.rebuild.EmploymentListResultBean;
import com.eqdd.yiqidian.bean.rebuild.GroupListResultBean;
import com.eqdd.yiqidian.utils.GreenDaoUtil;
import com.eqdd.yiqidian.utils.IMUtil;
import com.eqdd.yiqidian.utils.ImageUtil;

import java.util.ArrayList;
import java.util.List;

import io.rong.imkit.userInfoCache.RongUserInfoManager;
import io.rong.imkit.utilities.RongUtils;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.MessageContent;
import io.rong.imlib.model.SearchConversationResult;
import io.rong.imlib.model.UserInfo;

/**
 * @author吕志豪 .
 * @date 17-10-25  下午3:27.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class SearchActivity extends BaseActivity {

    private SearchActivityCustom dataBinding;
    private SlimAdapterEx<SearchBean> slimAdapterEx;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_search);
        initTopTitleBar(View.VISIBLE, "聊天信息搜索");
    }

    @Override
    public void initData() {
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create()
                .register(R.layout.list_item_26, new SlimInjector<SearchBean>() {
                    @Override
                    public void onInject(SearchBean data, IViewInjector injector) {
                        injector.text(R.id.tv_upper_content, data.getName())
                                .text(R.id.tv_down_content, data.getContent())
                                .imageCircle(R.id.iv_head, data.getUrl());
                    }
                })
                .attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    if (slimAdapterEx.getDataItem(position).getUserInfo() != null) {
                        IMUtil.startPrivate(SearchActivity.this, slimAdapterEx.getDataItem(position).getUserInfo());
                    } else {
                        IMUtil.startGroup(SearchActivity.this, slimAdapterEx.getDataItem(position).getGroupId(), slimAdapterEx.getDataItem(position).getName());

                    }
                });
        dataBinding.floatingSearchView.setOnQueryChangeListener((oldQuery, newQuery) -> {
            if (!TextUtils.isEmpty(newQuery)) {
                search(newQuery);
            }
        });
    }

    @Override
    public void setView() {

    }

    private void search(String query) {
        RongIMClient.getInstance().searchConversations(query,
                new Conversation.ConversationType[]{Conversation.ConversationType.PRIVATE, Conversation.ConversationType.GROUP},
                new String[]{"RC:TxtMsg", "RC:VcMsg", "RC:ImgTextMsg", "RC:LBSMsg", "RC:FileMsg", "RC:ImgMsg", "EQD:mp"},
                new RongIMClient.ResultCallback<List<SearchConversationResult>>() {
                    @Override
                    public void onSuccess(List<SearchConversationResult> searchConversationResults) {
                        ArrayList<SearchBean> searchBeans = new ArrayList<>();

                        for (SearchConversationResult searchConversationResult : searchConversationResults) {
                            UserInfo userInfo = searchConversationResult.getConversation().getLatestMessage().getUserInfo();
                            String content = searchConversationResult.getConversation().getLatestMessage().getSearchableWord().get(0);
                            if (searchConversationResult.getConversation().getConversationType() == Conversation.ConversationType.PRIVATE) {
                                searchBeans.add(new SearchBean(content, userInfo.getName(), userInfo.getPortraitUri().toString()).setUserInfo(userInfo));
                            } else {
                                GroupListResultBean groupById = GreenDaoUtil.getGroupById(searchConversationResult.getConversation().getTargetId());
                                searchBeans.add(new SearchBean(String.format("包含:%s(%s)", userInfo.getName(), content), groupById.getGroupname(), groupById.getGroupphoto()).setGroupId(searchConversationResult.getConversation().getTargetId()));
                            }
                        }
                        slimAdapterEx.updateData(searchBeans);
                    }

                    @Override
                    public void onError(RongIMClient.ErrorCode errorCode) {

                    }
                });
    }

    public class SearchBean {
        String content;
        String name;
        String url;
        UserInfo userInfo;
        String groupId;

        public String getGroupId() {
            return groupId;
        }

        public SearchBean setGroupId(String groupId) {
            this.groupId = groupId;
            return this;
        }

        public UserInfo getUserInfo() {
            return userInfo;
        }

        public SearchBean setUserInfo(UserInfo userInfo) {
            this.userInfo = userInfo;
            return this;
        }

        public SearchBean(String content, String name, String url) {
            this.content = content;
            this.name = name;
            this.url = url;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }
}
